home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 39.1 KB | 1,395 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UMacAppGlobals.cp
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UMACAPPGLOBALS__
- #include "UMacAppGlobals.h"
- #endif
-
- // MacApp
-
- // #ifndef __UAPPLICATION__
- // #include "UApplication.h"
- // #endif
-
- #ifndef __UBUSYCURSOR__
- #include "UBusyCursor.h"
- #endif
-
- #ifndef __UCLIPBOARDMGR__
- #include "UClipboardMgr.h"
- #endif
-
- #if qContainer
- #ifndef __UCONTAINER__
- #include "UContainer.h"
- #endif
- #endif
-
- #ifndef __UCONTROL__
- #include "UControl.h"
- #endif
-
- #ifndef __UDEBUG__
- #include "UDebug.h"
- #endif
-
- // #ifndef __UDIALOG__
- // #include "UDialog.h"
- // #endif
-
- #ifndef __UDISPATCHER__
- #include "UDispatcher.h"
- #endif
-
- #ifndef __UDOCUMENT__
- #include "UDocument.h"
- #endif
-
- // always include for TDragDropBehavior stub
- #ifndef __UDRAGDROBEHAVIOR__
- #include "UDragDropBehavior.h"
- #endif
-
- #ifndef __UDRAWINGENVIRONMENT__
- #include "UDrawingEnvironment.h"
- #endif
-
- #ifndef __UERRORMGR__
- #include "UErrorMgr.h"
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include "UMacAppUtilities.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __UMENUMGR__
- #include "UMenuMgr.h"
- #endif
-
- #ifndef __UNIVERSALSTARTUP__
- #include "UniversalStartup.h"
- #endif
-
- #ifndef __UPATCH__
- #include "UPatch.h"
- #endif
-
- #ifndef __UPRINTHANDLER__
- #include "UPrintHandler.h"
- #endif
-
- #ifndef __USCRIPTING__
- #include "UScripting.h"
- #endif
-
- #ifndef __USCROLLER__
- #include "UScroller.h"
- #endif
-
- #ifndef __USEGMENTS__
- #include "USegments.h"
- #endif
-
- #ifndef __UUNDO__
- #include "UUndo.h"
- #endif
-
- #ifndef __UWINDOW__
- #include "UWindow.h"
- #endif
-
- // CALib
-
- #if qContainer
- #ifndef _CALIB_
- #include "CALib.h"
- #endif
- #endif
-
- // Toolbox
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __FOLDERS__
- #include <Folders.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
- #ifndef __GESTALT__
- #include <Gestalt.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- #ifndef __STDLIB__
- #include <stdlib.h>
- #endif
-
- //----------------------------------------------------------------------------------------
- #if !defined(__MWERKS__)
- QDGlobals qd;
- #endif
-
- TextStyle gSystemStyle;
- TextStyle gApplicationStyle;
- TextStyle gApplicationStyle9;
-
- WindowRef gWorkPort;
-
- RgnHandle gTemporaryRegion;
-
- RgnHandle CTemporaryRegion::fCachedRgn;
-
- NMRecPtr gNotificationPtr;
-
- ModalFilterYDProcPtr gModalFilterYDProcPtr = MacAppStandardFileFilter;
-
- #if qDebug
- Boolean gBusyTemporaryRegion;
- CStr255 gUsedBy;
- #endif
-
- FailInfo pFi;
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
- static pascal void ExitToShellCleanupMacApp(void);
- static short FindPos(const CStr255& pattern, CStr255& source);
-
- //========================================================================================
- // CLASS PatchExitToShell
- //========================================================================================
- #pragma segment MAGlobalsRes
-
- typedef pascal void (*ExitToShellType)(void);
-
- #if GENERATINGCFM
- typedef UniversalProcPtr ExitToShellUPP;
- #else
- typedef ProcPtr ExitToShellUPP;
- #endif
-
- enum
- {
- uppExitToShellProcInfo = kPascalStackBased
- };
-
- #if GENERATINGCFM
- #define NewExitToShellProc(userRoutine) \
- (ExitToShellUPP) NewRoutineDescriptor((ProcPtr)(userRoutine), uppExitToShellProcInfo, GetCurrentISA())
- #else
- #define NewExitToShellProc(userRoutine) \
- ((ExitToShellUPP) (userRoutine))
- #endif
-
- #if GENERATINGCFM
- #define CallExitToShellProc(userRoutine) \
- CallUniversalProc((UniversalProcPtr)(userRoutine), uppExitToShellProcInfo)
- #else
- #define CallExitToShellProc(userRoutine) \
- (*(userRoutine))()
- #endif
-
- class PatchExitToShell : public TrapPatch
- {
- public:
- void Install(ExitToShellType routine);
- void CallInherited(void);
- };
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void PatchExitToShell::Install(ExitToShellType routine)
- {
- patchRoutine = NewExitToShellProc(StripLong(routine));
- FailNIL(patchRoutine);
- PatchTrap(_ExitToShell, patchRoutine);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- void PatchExitToShell::CallInherited(void)
- {
- CallExitToShellProc((ExitToShellType) oldTrapAddr);
- }
-
- //--------------------------------------------------------------------------------------------------
-
- static PatchExitToShell pETSPatch;
-
- //========================================================================================
- // GLOBAL Functions
- //========================================================================================
-
- #if qOldConfigurationFlags
- static void DefineConfiguration(Configuration& theConfiguration);
- #endif
-
- //----------------------------------------------------------------------------------------
- // GetAlertButtonTitle: Retrieve the title of the button control. If itemNo isn't a
- // button, then return ''.
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- void GetAlertButtonTitle(DialogRef theDialog,
- short itemNo,
- CStr255& theTitle)
- {
- short itemType;
- Handle item;
- CRect box;
- GetDialogItem(theDialog, itemNo, &itemType, &item, &box);
-
- if (itemType == (ctrlItem + btnCtrl))
- GetControlTitle((ControlRef)item, theTitle);
- else
- theTitle.Empty();
- } // GetAlertButtonTitle
-
- //----------------------------------------------------------------------------------------
- // DoAlertKeyDown: Handle a keypress that has been mapped to one of the button controls.
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- void DoAlertKeyDown(DialogRef theDialog, short itemNo)
- {
- short itemType;
- Handle item;
- CRect box;
-
- GetDialogItem(theDialog, itemNo, &itemType, &item, &box);
- if (itemType == (ctrlItem + btnCtrl))
- {
- // this code gives visual feedback
- HiliteControl((ControlRef)item, kControlUpButtonPart); // hilite the button
-
- long finalTicks;
- Delay(8, &finalTicks); // delay for 8 ticks
-
- HiliteControl((ControlRef)item, kControlNoPart); // stop hiliting the button
- }
- } // DoAlertKeyDown
-
- //----------------------------------------------------------------------------------------
- // CompareAlertKeysToItem: Compares the buffered multi-byte chars to the first character
- // of each button title 1st button in alert (by convention == "OK"). 2nd button in alert
- // (by convention == "Cancel"). 3rd button in alert (by convention == "No")
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- Boolean CompareAlertKeysToItem(DialogRef theDialog,
- const CStr2& theChars,
- short& itemHit)
- {
- Boolean returnVal = FALSE;
-
- static const short itemtests[3] = { ok, cancel, kNoButton };
-
- for (short i = 0; i < 4; i++)
- {
- CStr255 title;
- GetAlertButtonTitle(theDialog, itemtests[i], title);
- if (CompareMultiByteChars(theChars, title, FALSE))
- {
- itemHit = itemtests[i];
- returnVal = TRUE;
- break;
- }
- }
-
- return returnVal;
- } // CompareAlertKeysToItem
-
- //----------------------------------------------------------------------------------------
- // DrawDefaultProc: Draw the thick rounded rectangle around the default button
- // (Imported to MacApp from Ponnuki by GA)
- //
- // This routine uses Keith Rollin's algorithm, as presented in the USENET Guide to
- // Programming the Macintosh. I have modified the basic algorithm only slightly--I add
- // two to the calculated 'buttonOval' value. This gets better results, particularly for
- // buttons of the default size (18 points).
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- void DrawDefaultProc(DialogRef dlog)
- {
- // Don't call GetDialogItem if the default button # has a strange value
- short defaultButton = GetDialogDefaultItem(dlog);
- if( defaultButton > 0 )
- {
- GrafPtr savePort;
- short type = 0;
- ControlRef buttonHandle = NULL;
- CRect outlineBox;
-
- GetPort(&savePort);
- SetPortWindowPort( dlog );
-
- GetDialogItem(dlog, defaultButton, &type, (Handle *) &buttonHandle, outlineBox);
- outlineBox.Inset(CPoint(-4,-4));
-
- // Don't draw the bold outline around the default button
- // unless it really is a button
- if( type == ctrlItem + btnCtrl )
- {
- PenState saveState;
- short buttonOval;
-
- GetPenState( &saveState );
-
- // We want to draw the thick line with a normal
- // pen pattern that is 3 pixels wide
- PenNormal();
- PenSize(3,3);
-
- // If the button we are outlining is disabled,
- // draw the outline with a gray pattern.
- //•••SRF - Actually, on color systems we should use disable gray mode
- if( (*buttonHandle)->contrlHilite == 255 )
- PenPat(&qd.gray);
-
-
- // Calculate the curvature to use and draw the thick line
- buttonOval = 2 + (outlineBox.bottom - outlineBox.top) / 2;
- FrameRoundRect(outlineBox,buttonOval,buttonOval);
-
- SetPenState( &saveState );
- }
- SetPort(savePort);
- }
- } // DrawDefaultProc
-
- //----------------------------------------------------------------------------------------
- // MinimalAlertFilter: This is the minimally-functional alert filter that MacApp uses
- // in cases where gDispatcher has not yet been initialized. TApplication.AlertFilter
- // calls this filter as well.
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- pascal Boolean MinimalAlertFilter(DialogRef theDialog,
- EventRecord& theEvent,
- short& itemHit)
- {
- Boolean returnValue = FALSE;
-
- switch(theEvent.what)
- {
- case updateEvt:
- if (((DialogRef)theEvent.message) == theDialog)
- DrawDefaultProc(theDialog);
- break;
-
- case keyDown:
- {
- static CStr2 bufferedCharacter; // hangs around in between keystrokes
- unsigned char theChar = (unsigned char)(((theEvent.message) & charCodeMask));
-
- Boolean processCharacter = TRUE;
- switch (MACharacterByteType((Ptr)&theChar, 0, smCurrentScript))
- {
- case smFirstByte:
- processCharacter = FALSE;
- // no break - just drop through
-
- case smSingleByte:
- bufferedCharacter = theChar;
- break;
-
- case smLastByte:
- bufferedCharacter += theChar;
- break;
- }
-
- if (processCharacter)
- {
- if ((bufferedCharacter == chEnter) || (bufferedCharacter == chReturn))
- {
- itemHit = GetDialogDefaultItem(theDialog);
- DoAlertKeyDown(theDialog, itemHit);
- returnValue = TRUE;
- }
- else if ( (bufferedCharacter == '`') || (bufferedCharacter == chEscape) || ((bufferedCharacter == '.') && (theEvent.modifiers & cmdKey)))
- {
- DoAlertKeyDown(theDialog, cancel);
- itemHit = cancel;
- returnValue = TRUE;
- }
- }
-
- break;
- }
- }
-
- return returnValue;
- } // MinimalAlertFilter
-
- //----------------------------------------------------------------------------------------
- // MacAppAlertFilter:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- pascal Boolean MacAppAlertFilter(DialogRef theDialog,
- EventRecord& theEvent,
- short& itemHit)
- {
- return gDispatcher ? gDispatcher->AlertFilter(theDialog, theEvent, itemHit) : MinimalAlertFilter(theDialog, theEvent, itemHit);
- } // MacAppAlertFilter
-
- //----------------------------------------------------------------------------------------
- // MacAppAppleEventIdleProc:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- pascal Boolean MacAppAppleEventIdleProc(EventRecord& theEventRecord,
- long& sleepTime,
- RgnHandle& mouseRgn)
- {
- if (gDispatcher)
- return gDispatcher->AppleEventIdleProc(theEventRecord, sleepTime, mouseRgn);
- else
- return FALSE;
- } // MacAppAppleEventIdleProc
-
- //----------------------------------------------------------------------------------------
- // MacAppStandardFileFilter:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- pascal Boolean MacAppStandardFileFilter(DialogRef theDialog,
- EventRecord* theEvent,
- short* itemHit,
- void *yourDataPtr)
- {
- if (gDispatcher)
- return gDispatcher->StandardFileFilter(theDialog, *theEvent, *itemHit, yourDataPtr);
- else
- return FALSE;
- } // MacAppStandardFileFilter
-
- //----------------------------------------------------------------------------------------
- // ApplicationBeep:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- void ApplicationBeep()
- {
- if (gDispatcher)
- gDispatcher->Beep(2);
- else
- SysBeep(2);
- } // ApplicationBeep
-
- //----------------------------------------------------------------------------------------
- // CleanupMacApp:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- void CleanupMacApp(void)
- {
- SetResLoad(TRUE); // Make sure segments can load
-
- pETSPatch.UnpatchTrap(); // Guaranteed not to fail
-
- if (gDispatcher)
- gDispatcher->Terminate();
-
- TBusyCursor::TerminateUBusyCursor();
-
- /*
- #if qContainer
- if (gContainerLib)
- {
- CAShutdown();
- }
- #endif
- */
-
- AEDisposeDesc(&gServerAddress);
-
- if (FailInfo::gTopHandler)
- FailInfo::gTopHandler->Success(); // Remove the outermost failure handler
-
- // #if qDebug
- // DebugTerminate();
- // #endif
-
- UnpatchAll();
-
- {
- // Clear gDispatcher before freeing so nobody can
- // refer to the object while it is being destructed.
- TDispatcher* theDispatcher = gDispatcher;
- gDispatcher = NULL;
- FreeIfObject(theDispatcher);
- }
-
- TOSADispatcher::fgDispatcher = (TOSADispatcher *)FreeIfObject(TOSADispatcher::fgDispatcher);
- } // CleanupMacApp
-
- //----------------------------------------------------------------------------------------
- // ExitToShellCleanupMacApp:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- pascal void ExitToShellCleanupMacApp()
- {
- long OldA5 = SetCurrentA5(); // ***** Called from trap patches *****
-
- ExitToShellType oldTrapAddr = (ExitToShellType) pETSPatch.GetOldTrapAddr();
- CleanupMacApp();
- CallExitToShellProc(oldTrapAddr);
-
- SetA5(OldA5);
- } // ExitToShellCleanupMacApp
-
- //----------------------------------------------------------------------------------------
- // DoneWithTemporaryRegion: Indicates that gTemporaryRegion is no longer in use. Call this
- // only if qDebug is true.
- //----------------------------------------------------------------------------------------
- #if qDebug
- #pragma segment MADebug
-
- void DoneWithTemporaryRegion()
- {
- if (!gBusyTemporaryRegion)
- ProgramBreak("DoneWithTemporaryRegion called, but gTemporaryRegion is not locked");
- gBusyTemporaryRegion = FALSE;
- gUsedBy.Empty();
- SetEmptyRgn(gTemporaryRegion);
- } // DoneWithTemporaryRegion
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // EnterDebugger:
- //----------------------------------------------------------------------------------------
- #if qDebug
- #pragma segment MADebug
-
- void EnterDebugger(Boolean entering)
- {
- if (TBusyCursor::fgBusyCursor)
- TBusyCursor::fgBusyCursor->Activate(!entering);
- } // EnterDebugger
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // ExitMacApp:
- //----------------------------------------------------------------------------------------
- #pragma segment MATerminate
-
- void ExitMacApp()
- {
- ExitToShell();
- // patch will call CleanupMacApp
-
- } // ExitMacApp
-
- //----------------------------------------------------------------------------------------
- // GetNewCenteredDialog:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- DialogRef GetNewCenteredDialog(ResNumber dialogID,
- Ptr dStorage,
- WindowRef behind)
- {
- DialogTHndl dlogTemplate;
-
- SetCursor(&(qd.arrow));
-
- if (gDispatcher)
- gDispatcher->InvalidateMouseRegions();
-
- dlogTemplate = (DialogTHndl)GetResource('DLOG', dialogID);
- if (dlogTemplate)
- {
- CenterRectOnScreen(((CRect &) (*dlogTemplate)->boundsRect), TRUE, TRUE, TRUE);
- return GetNewDialog(dialogID, dStorage, behind);
- }
- else
- {
- SysBeep(2); // At least give some indication
- #if qDebug
- CStr255 theString;
- ConcatNumber("Unable to find 'DLOG' resource ", dialogID, theString);
- ProgramBreak(theString);
- #endif
-
- }
- return NULL;
- } // GetNewCenteredDialog
-
- //----------------------------------------------------------------------------------------
- // GetTextStyleFontInfo: Really a utility, but the gWorkPort isn't reachable from
- // UMacAppUtilities
- //----------------------------------------------------------------------------------------
- #pragma segment MAUtilitiesRes
-
- void GetTextStyleFontInfo(const TextStyle& theTextStyle,
- FontInfo& theFontInfo,
- short& theFontHeight)
- {
- GrafPtr savedPort;
-
- GetPort(&savedPort);
- SetPortWindowPort(gWorkPort);
- SetPortTextStyle(theTextStyle);
- theFontHeight = MAGetFontInfo(theFontInfo);
- SetPort(savedPort);
- } // GetTextStyleFontInfo
-
- #if qOldConfigurationFlags
-
- //----------------------------------------------------------------------------------------
- // DefineConfiguration:
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- void DefineConfiguration(Configuration& theConfiguration)
- {
- theConfiguration.environsVersion = GetEnvironsVersion();
- theConfiguration.machineType = GetMachineType();
- theConfiguration.systemVersion = GetSystemVersion();
- theConfiguration.processor = GetProcessor();
- theConfiguration.hasFPU = HasFPU();
- theConfiguration.hasColorQD = HasColorQD();
- theConfiguration.has32BitQD = Has32BitQD();
- theConfiguration.keyboardType = KeyboardType();
- theConfiguration.atDrvrVersNum = GetATDrvrVersNum();
- theConfiguration.hasSCSI = HasSCSI();
- theConfiguration.hasAUX = HasAUX();
- theConfiguration.hasTempMem = HasTempMem();
- theConfiguration.teVersion = GetTEVersion();
- theConfiguration.hasDesktopBus = HasDesktopBus();
- theConfiguration.hasTrueType = HasTrueType();
- theConfiguration.isOnlyBackground = IsOnlyBackground();
- theConfiguration.isHighLevelEventAware = IsHighLevelEventAware();
- theConfiguration.hasDragManager = HasDragManager();
- theConfiguration.hasThreadManager = HasThreadManager();
- theConfiguration.hasAOCEToolBox = HasAOCEToolBox();
- theConfiguration.hasQDGXGraphics = HasQDGXGraphics();
- theConfiguration.hasQDGXPrinting = HasQDGXPrinting();
- #if 0
- // Removed due to ASLM removal vs. deinstall problems.
- theConfiguration.hasASLM = HasASLM();
- #endif
- theConfiguration.hasCFM = HasCFM();
- theConfiguration.hasTranslationManager = HasTranslationManager();
- theConfiguration.hasSpeechManager = HasSpeechManager();
- theConfiguration.hasCustomFile = HasCustomFile();
- theConfiguration.hasQuickTime = HasQuickTime();
- theConfiguration.hasTSM = HasTSM();
- theConfiguration.hasAppleScript = HasAppleScript();
- }
-
- #endif // qOldConfigurationFlags
-
- //----------------------------------------------------------------------------------------
- // InitUMacApp_Step1
- //----------------------------------------------------------------------------------------
- #pragma push
- #pragma processor 68000
- // Needs to be universal code
- #pragma segment Main
- // Must be in the Main segment since all other segments get unloaded from here.
-
- void InitUMacApp_Step1()
- {
- UniversalStartup(); // remove this to static initialization when possible
-
- // At this point the processor has been verified and "universal" code is no longer
- // necessary when on 68k machines. Also, the toolbox has been initialized
-
- #if qOldConfigurationFlags
- DefineConfiguration(gConfiguration);
- #endif
- if (!ValidateConfiguration())
- AlertUnsupportedConfiguration();
-
- // Init the basics used by the ErrorMgr
- gMacAppAlertFilter = (ProcPtr)MacAppAlertFilter;
- } // InitUMacApp_Step1
-
- #pragma pop
-
- //----------------------------------------------------------------------------------------
- // InitUMacApp_Step3
- //----------------------------------------------------------------------------------------
- #pragma segment Main
- void InitUMacApp_Step3(short callsToMoreMasters)
- {
- // Install MacApp's Memory management systems
- InitCoreMacApp(callsToMoreMasters);
-
- DoInitUMacApp(); // do rest of initialization
-
- #if qSegments
- UnloadAllSegments();
- #endif
- } // InitUMacApp_Step3
-
- //----------------------------------------------------------------------------------------
- // DoInitUMacApp: Must be in the init segment; unloaded at start of event loop
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void DoInitUMacApp()
- {
- // Initialize runtime support for objects
- InitUObject();
- #if qDebug || qTheDebugger
- InitUDebug();
- #endif
-
- // assign the global text style records from resources
- MAGetTextStyle(kSystemFontTextStyle, gSystemStyle);
- MAGetTextStyle(kApplFont12TextStyle, gApplicationStyle);
- MAGetTextStyle(kApplFont9TextStyle, gApplicationStyle9);
-
- // atexit(&CleanupMacApp);
- pETSPatch.Install(ExitToShellCleanupMacApp);
-
- // Initialize the view system
- Boolean initGX = FALSE;
- #if qGXViews || qGXPrinting
- initGX = TRUE;
- #endif
- TView::InitUView(initGX);
-
- // Other 1-time initialization
- gTemporaryRegion = MakeNewRgn();
-
- // Create a work port for our convenience
- if (qNeedsColorQD || HasColorQD())
- gWorkPort = NewCWindow(NULL, gZeroRect, gEmptyString, FALSE, documentProc, NULL, FALSE, 0);
- else
- gWorkPort = NewWindow(NULL, gZeroRect, gEmptyString, FALSE, documentProc, NULL, FALSE, 0);
-
- #if !qModelCFM
- TBusyCursor::InitUBusyCursor();
- #endif
-
- #if qContainer
- InstallContainerLib();
- #endif
-
- TPrintHandler::gNullPrintHandler = new TPrintHandler;
- TPrintHandler::gNullPrintHandler->IPrintHandler(NULL);
-
- TPrintHandler::gPrintHandler = TPrintHandler::gNullPrintHandler;
-
- #if qTemplateViews
- {
- // ===============================================
- // Suppress Linker dead stripping of these classes
-
- MA_REGISTER_CLASS(TDragDropBehavior);
-
- // ===============================================
-
- MA_REGISTER_SIGNATURE(TView, kStdView);
- MA_REGISTER_SIGNATURE(TView, kStdDefaultView);
- MA_REGISTER_SIGNATURE(TIncludeView, kStdIncludeAt);
- MA_REGISTER_SIGNATURE(TWindow, kStdWindow);
- MA_REGISTER_SIGNATURE(TScrollBar, kStdScrollBar);
- MA_REGISTER_SIGNATURE(TScrollerScrollBar, kStdScrollerScrollBar);
- MA_REGISTER_SIGNATURE(TScroller, kStdScroller);
-
- MA_REGISTER_SIGNATURE(TTracker, kStdTracker);
- MA_REGISTER_SIGNATURE(TList, kStdList);
- MA_REGISTER_SIGNATURE(TDrawingEnvironment, kStdDrawingEnvironment);
- }
- #endif
-
- #if qDebug
- // The linker optimizes away the meta information for the following classes by
- // stripping the ClassInfoProc. MacApp does a SetEltType on lists containing these
- // classes which requires meta information. Suppress the optimization by doing
- // suppressing dead stripping. (See UApplication.cp)
-
- MA_REGISTER_CLASS(TDocument);
- MA_REGISTER_CLASS(TEvent);
- MA_REGISTER_CLASS(TClientCommand);
- #endif
-
- InitUAdorners(); // Temporarily needed for debug window
-
- TUndoHandler::InitUUndo();
-
- InitUMenuMgr();
- InitUClipboardMgr();
-
- // create gServerAddress
- ProcessSerialNumber psn;
- psn.highLongOfPSN = 0;
- psn.lowLongOfPSN = kCurrentProcess;
- FailOSErr(AECreateDesc(typeProcessSerialNumber, (Ptr) &psn, sizeof(ProcessSerialNumber), &gServerAddress));
-
- // initialize the pseudo-random sequence generation function
- unsigned long time;
- GetDateTime(&time);
- srand((unsigned int)time);
-
- #if qDebug
- //InitUDialog(); //Necessary for debug dialog.
- #if qPerform
- InitUTEView();
- #endif
- #endif
-
- } // DoInitUMacApp
-
- //----------------------------------------------------------------------------------------
- // InstallIfPrintHandler:
- //----------------------------------------------------------------------------------------
- #pragma segment MARes
-
- void InstallIfPrintHandler(TPrintHandler* aPrintHandler,
- TView* aView)
-
- {
- // Install a copy of aPrintHandler into the view. aPrintHandler will be a real printhandler
- // if UPrinting has been initialized otherwise it is a NULL print handler.
- if ((aPrintHandler) && (aView) && (aPrintHandler != TPrintHandler::gNullPrintHandler) && (TPrintHandler::gPrintHandler != TPrintHandler::gNullPrintHandler))
- {
- TPrintHandler * aNewPrintHandler = (TPrintHandler *)aPrintHandler->Clone();// signals failure if can't clone
-
- aNewPrintHandler->fView = aView;
- if (aView->fDocument)
- aNewPrintHandler->fDocument = aView->fDocument; // so the printinfo can be shared if necessary
-
- aNewPrintHandler->SetDefaultPrintInfo(); // do this after we have a document (if any)
-
- //
- // Now do the attachments
- //
- if (aView->fDocument)
- aView->fDocument->AttachPrintHandler(aNewPrintHandler);
- aView->AttachPrintHandler(aNewPrintHandler);
- }
- } // InstallIfPrintHandler
-
- //----------------------------------------------------------------------------------------
- // MakeNewRgn:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- RgnHandle MakeNewRgn()
- {
- RgnHandle aRgn = NewRgn();
- FailNIL(aRgn);
- return aRgn;
- } // MakeNewRgn
-
- //----------------------------------------------------------------------------------------
- // DetermineClassDesc:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- const ClassDesc* DetermineClassDesc(IDType signature, const ClassName& className)
- {
- if (className.IsEmpty())
- return ClassDesc::GetClassDescFromSignature(signature);
- else
- return ClassDesc::GetClassDescFromClassName(className);
- } // DetermineClassDesc
-
- //----------------------------------------------------------------------------------------
- // GetClassDescFromSignature:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- const ClassDesc* GetClassDescFromSignature(IDType signature)
- {
- return ClassDesc::GetClassDescFromSignature(signature);
- }
-
- //----------------------------------------------------------------------------------------
- // NewStdObject:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- TObject* NewStdObject(IDType signature)
- {
- return (TObject*) ClassDesc::NewBySignature(signature);
- }
-
- //----------------------------------------------------------------------------------------
- // NewObjectBySignature:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
-
- TObject* NewObjectBySignature(IDType signature,
- const ClassName& className)
- {
- if (className.IsEmpty())
- return (TObject*) ClassDesc::NewBySignature(signature);
- else
- return (TObject*) ClassDesc::NewByClassName(className);
- }
-
- //----------------------------------------------------------------------------------------
- // FindPos:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- short FindPos(const CStr255& pattern, CStr255& source)
- {
- short i = 0;
- short j = 0;
- short position = 0;
-
- do
- {
- ++i;
- position = i;
- for (j = 1; j <= pattern.Length(); ++j)
- if (!((source[i + j - 1] == pattern[j]) && (MACharacterByteType((Ptr)&source[1], i + j - 2, smCurrentScript) == MACharacterByteType((Ptr)&pattern[1], j - 1, smCurrentScript))))
- {
- position = 0;
- break;
- }
- } while (!((position > 0) || (i >= source.Length() - pattern.Length() + 1)));
-
- return position;
- } // FindPos
-
-
- //----------------------------------------------------------------------------------------
- // ParseTitleTemplate:
- //----------------------------------------------------------------------------------------
- Boolean ParseTitleTemplate(CStr255& itsTemplate,
- short& preDocname,
- short& constTitle)
-
- {
- const CStr255 kPreDocname = "<<<";
- const short kPreSize = 3;
- const CStr255 kPostDocname = ">>>";
- const short kPostSize = 3;
-
- if (itsTemplate.IsEmpty())
- {
- preDocname = 1;
- constTitle = 0;
- }
- else
- {
- preDocname = FindPos(kPreDocname, itsTemplate);
- if (preDocname > 0)
- {
- itsTemplate.Delete(preDocname, kPreSize);
- short x = FindPos(kPostDocname, itsTemplate);
- if (x == 0)
- constTitle = preDocname - 1;
- else
- {
- itsTemplate.Delete(x, kPostSize);
- constTitle = itsTemplate.Length() - x + preDocname;
- }
- }
- }
-
- return preDocname > 0;
- } // ParseTitleTemplate
-
- //----------------------------------------------------------------------------------------
- // RegisterStdType: Register or re-register a type and a class
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- void RegisterStdType(const CStr255& typeName,
- IDType signature)
- {
- // If the name can't be found it was probably misspelled or dead-stripped
- const ClassDesc* itsClassDesc = ClassDesc::GetClassDescFromClassName(typeName);
- if (itsClassDesc == NULL)
- Failure(minErr, 0);
-
- // break const-ness
- ((ClassDesc*) itsClassDesc)->RegisterSignature(signature);
- }
-
- //----------------------------------------------------------------------------------------
- // GetStandardType:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- IDType GetStandardType(const ClassDesc* itsClassDesc)
- {
- return (itsClassDesc != NULL) ? itsClassDesc->GetSignature() : kNoIdentifier;
- }
-
- //----------------------------------------------------------------------------------------
- // SubstituteInTitle:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- Boolean SubstituteInTitle(CStr255& title,
- const CStr255& newStuff,
- short preDocname,
- short constTitle)
- {
- if (preDocname > 0)
- {
- if (constTitle == 0)
- title = newStuff;
- else
- {
- title.Delete(preDocname, title.Length() - constTitle);
- title.Insert(newStuff, preDocname);
- }
- return TRUE;
- }
- else
- return FALSE;
- } // SubstituteInTitle
-
- //----------------------------------------------------------------------------------------
- // UseTemporaryRegion: Call this when you are about to use gTemporaryRegion and qDebug is
- // true. Used with DoneWithTemporaryRegion will prevent you from trying to use
- // gTemporaryRegion from two places at the same time.
- //----------------------------------------------------------------------------------------
- #if qDebug
- #pragma segment MADebug
-
- void UseTemporaryRegion(const CStr255& byWhom)
- {
- if (gBusyTemporaryRegion)
- {
- fprintf(stderr, "'%s' is trying to lock gTemporaryRegion,", (const char*)byWhom);
- fprintf(stderr, "but it is already locked by '%s'\n", (const char*)gUsedBy);
- ProgramBreak("Error in UseTemporaryRegion");
- }
- else
- {
- gBusyTemporaryRegion = TRUE;
- gUsedBy = byWhom;
- }
- } // UseTemporaryRegion
-
- #endif
-
-
- //========================================================================================
- // CLASS CTemporaryRegion
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // CTemporaryRegion::new:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- void* CTemporaryRegion::operator new(size_t /*size*/)
- {
- return NULL; // prevent heap-based allocation
- }
-
- //----------------------------------------------------------------------------------------
- // CTemporaryRegion::delete:
- //----------------------------------------------------------------------------------------
- #pragma segment IteratorRes
-
- void CTemporaryRegion::operator delete(void*)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CTemporaryRegion::CTemporaryRegion:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- CTemporaryRegion::CTemporaryRegion()
- {
- // Simple single level cache for first cut
- // take the cached region if available otherwise make one up to give out
- if (fCachedRgn)
- {
- fRgnHandle = fCachedRgn;
- fCachedRgn = NULL;
- }
- else
- {
- fRgnHandle = MakeNewRgn();
- }
-
- // Setup the FailInfo to catch any failures while this object is in scope
- fFailInfo.SetCleanupProc(CTemporaryRegion::CallCleanup, this);
- } // CTemporaryRegion::CTemporaryRegion
-
- //----------------------------------------------------------------------------------------
- // CTemporaryRegion::~CTemporaryRegion:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- CTemporaryRegion::~CTemporaryRegion()
- {
- this->Cleanup();
-
- fFailInfo.Success();
- } // CTemporaryRegion::~CTemporaryRegion
-
- //----------------------------------------------------------------------------------------
- // CTemporaryRegion::Cleanup:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- void CTemporaryRegion::Cleanup()
- {
- // return to the cache if not already full
- if (fCachedRgn)
- {
- fRgnHandle = DisposeIfRgnHandle(fRgnHandle);
- }
- else
- {
- fCachedRgn = fRgnHandle;
- fRgnHandle = NULL;
- }
- } // CTemporaryRegion::Cleanup
-
- //----------------------------------------------------------------------------------------
- // CTemporaryRegion::CallCleanup:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- /*static*/ void CTemporaryRegion::CallCleanup(void* context)
- {
- ((CTemporaryRegion*) context)->Cleanup();
- } // CTemporaryRegion::CallCleanup
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // GetSysVolume:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- OSErr GetSysVolume(short& vRefNum)
- {
- long dir;
-
- return FindFolder(kOnSystemDisk, kSystemFolderType, FALSE, &vRefNum, &dir);
- } // GetSysVolume
-
-
- //----------------------------------------------------------------------------------------
- // GetIndVolume:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- OSErr GetIndVolume(short index,
- short& vRefNum)
- {
- ParamBlockRec pb;
- OSErr err;
-
- pb.volumeParam.ioCompletion = NULL;
- pb.volumeParam.ioNamePtr = NULL;
- pb.volumeParam.ioVolIndex = index;
-
- err = PBGetVInfoSync(&pb);
-
- vRefNum = pb.volumeParam.ioVRefNum;
- return err;
- } // GetIndVolume
-
-
- //----------------------------------------------------------------------------------------
- // VolHasDesktopDB:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- OSErr VolHasDesktopDB(short vRefNum, Boolean& hasDesktop)
- {
- HParamBlockRec pb;
- GetVolParmsInfoBuffer info;
-
- pb.ioParam.ioCompletion = NULL;
- pb.ioParam.ioVRefNum = vRefNum;
- pb.ioParam.ioNamePtr = NULL;
- pb.ioParam.ioBuffer = (Ptr) & info;
- pb.ioParam.ioReqCount = sizeof(GetVolParmsInfoBuffer);
-
- OSErr err = ::PBHGetVolParmsSync(&pb);
-
- hasDesktop = (err == noErr) && ((info.vMAttrib & (1L << bHasDesktopMgr)) != 0);
-
- return err;
-
- } // VolHasDesktopDB
-
-
- //----------------------------------------------------------------------------------------
- // FindAppOnVolume:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- OSErr FindAppOnVolume(OSType sig,
- short vRefNum,
- FSSpec& thefile)
- {
- DTPBRec pb;
- OSErr err;
-
- pb.ioCompletion = NULL;
- pb.ioVRefNum = vRefNum;
- pb.ioNamePtr = NULL;
- if ((err = PBDTGetPath(&pb)) != noErr)
- return err; // Puts DT refnum into pb.ioDTRefNum
- short refNum = pb.ioDTRefNum;
-
- pb.ioCompletion = NULL;
- pb.ioDTRefNum = refNum;
- pb.ioIndex = 0;
- pb.ioFileCreator = sig;
- pb.ioNamePtr = (StringPtr) thefile.name;
- err = PBDTGetAPPLSync(&pb); // Find it!
-
- if( err == fnfErr )
- err = afpItemNotFound; // Bug in PBDTGetAPPL
- if( err )
- return err; // Returns afpItemNotFound if app wasn't found.
-
- thefile.vRefNum = vRefNum;
- thefile.parID = pb.ioAPPLParID;
- return err;
- } // FindAppOnVolume
-
- //----------------------------------------------------------------------------------------
- // LaunchAppByFSSpec:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- OSErr LaunchAppByFSSpec(const FSSpec& fileSpec,
- LaunchFlags launchControlFlags,
- ProcessSerialNumber& psn)
- {
- LaunchParamBlockRec pb;
- OSErr err;
- FSSpec localFSSpec = fileSpec; // so we can take its address below
-
- pb.launchBlockID = extendedBlock;
- pb.launchEPBLength = extendedBlockLen;
- pb.launchFileFlags = launchNoFileFlags;
- pb.launchControlFlags = launchControlFlags | launchNoFileFlags;
- pb.launchAppSpec = &localFSSpec;
- pb.launchAppParameters = NULL;
-
- err = LaunchApplication(&pb);
- if (err == noErr)
- psn = pb.launchProcessSN;
- return err;
- } // LaunchAppByFSSpec
-
-
- //----------------------------------------------------------------------------------------
- // LaunchBySignature:
- //----------------------------------------------------------------------------------------
- #pragma segment MAGlobalsRes
-
- OSErr LaunchBySignature(OSType sig,
- ProcessSerialNumber& psn,
- FSSpec* fileSpec,
- Boolean* launched,
- Boolean allowLaunch,
- LaunchFlags launchControlFlags)
- {
- OSErr err;
- short sysVRefNum;
-
- // First see if it's already running:
-
- if (launched)
- *launched = FALSE;
- err = FindProcessBySignature(sig, psn, fileSpec);
-
- if (err == noErr)
- if ((launchControlFlags & launchDontSwitch) == 0)
- return SetFrontProcess(&psn); // They wanted to switch to it…
-
- if (err != procNotFound || !allowLaunch)
- return err;
-
- // Well, it's not running but it's okay to launch it. Let's have a look around:
-
- if ((err = GetSysVolume(sysVRefNum)) != noErr)
- return err; // Find boot volume
- short vRefNum = sysVRefNum; // Start search with boot volume
- short index = 0;
- do
- {
- if (index == 0 || vRefNum != sysVRefNum)
- {
- Boolean hasDesktopDB;
-
- if ((err = VolHasDesktopDB(vRefNum, hasDesktopDB)) != noErr)
- return err;
- if (hasDesktopDB)
- {
- FSSpec file;
-
- // If volume has a desktop DB,
- err = FindAppOnVolume(sig, vRefNum, file); // ask it to find app
- if (err == noErr)
- {
- // If found,
- if (fileSpec)
- *fileSpec = file;
- if (launched)
- *launched = TRUE;
- return LaunchAppByFSSpec(file, launchControlFlags, psn); // Launch it!
- }
- else if (err != afpItemNotFound && err != paramErr /* in case of Volume with no files (audio CD) */)
- return err;
- }
- }
- err = GetIndVolume(++index, vRefNum); // Else go to next volume
- } while (err == noErr); // Keep going until we run out of vols
-
- if( err==nsvErr || err==afpItemNotFound )
- err= fnfErr; // File not found on any volume
- return err;
- } // LaunchBySignature
-
- //----------------------------------------------------------------------------------------
- // End of UMacAppGlobals.cp
-
- #pragma segment Inline
-